// pages/substitute-class/publish/publish.js
Page({
  data: {
    formData: {
      courseName: '',
      teacher: '',
      location: '',
      date: '',
      startTime: '',
      endTime: '',
      reward: '',
      description: '',
      urgent: false,
      requirements: ''
    },
    dateRange: [],
    timeRange: []
  },

  onLoad() {
    // 设置日期范围（今天到30天后）
    const today = new Date()
    const endDate = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000)
    
    this.setData({
      dateRange: [
        this.formatDate(today),
        this.formatDate(endDate)
      ]
    })
  },

  formatDate(date) {
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
  },

  onInputChange(e) {
    const { field } = e.currentTarget.dataset
    const { value } = e.detail
    
    this.setData({
      [`formData.${field}`]: value
    })
  },

  onDateChange(e) {
    this.setData({
      'formData.date': e.detail.value
    })
  },

  onTimeChange(e) {
    const { field } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },

  onUrgentChange(e) {
    this.setData({
      'formData.urgent': e.detail.value
    })
  },

  onChooseImage() {
    wx.chooseImage({
      count: 3,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log('选择的图片:', res.tempFilePaths)
        // 这里可以上传图片到服务器
      }
    })
  },

  validateForm() {
    const { formData } = this.data
    
    if (!formData.courseName.trim()) {
      wx.showToast({ title: '请输入课程名称', icon: 'none' })
      return false
    }
    
    if (!formData.teacher.trim()) {
      wx.showToast({ title: '请输入教师姓名', icon: 'none' })
      return false
    }
    
    if (!formData.location.trim()) {
      wx.showToast({ title: '请输入上课地点', icon: 'none' })
      return false
    }
    
    if (!formData.date) {
      wx.showToast({ title: '请选择上课日期', icon: 'none' })
      return false
    }
    
    if (!formData.startTime || !formData.endTime) {
      wx.showToast({ title: '请选择上课时间', icon: 'none' })
      return false
    }
    
    if (!formData.reward || formData.reward <= 0) {
      wx.showToast({ title: '请输入有效的报酬金额', icon: 'none' })
      return false
    }
    
    if (!formData.description.trim()) {
      wx.showToast({ title: '请输入需求描述', icon: 'none' })
      return false
    }
    
    return true
  },

  onSubmit() {
    if (!this.validateForm()) {
      return
    }

    wx.showLoading({ title: '发布中...' })
    
    // 模拟API调用
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            wx.navigateBack()
          }, 2000)
        }
      })
    }, 1500)
  },

  onPreview() {
    const { formData } = this.data
    
    if (!this.validateForm()) {
      return
    }

    const previewText = `课程：${formData.courseName}
教师：${formData.teacher}
地点：${formData.location}
时间：${formData.date} ${formData.startTime}-${formData.endTime}
报酬：¥${formData.reward}
描述：${formData.description}
${formData.urgent ? '【紧急】' : ''}`

    wx.showModal({
      title: '预览发布内容',
      content: previewText,
      confirmText: '确认发布',
      success: (res) => {
        if (res.confirm) {
          this.onSubmit()
        }
      }
    })
  }
})